SETUPLEN = 4
BOOTSEG = 0x7c0
INITSEG = 0x9000
SETUPSEG = 0x9020
SYSSEG = 0x1000
ENDSEG = SYSSEG + SYSSIZE
ROOT_DEV = 0x000

.code16
.text

.global _start
_start:
    # jmpl为段间跳转指令，表示跳转到BOOTSEG的标签start2处
    jmpl $BOOTSEG, $start2  # 跳转start2执行

# 将BOOSEG段复制到INITSEG段，避免被覆盖
start2:
    # x86汇编中不能直接将一个立即数转移到段寄存器中，因此需要经过通用寄存器ax中转
    movw $BOOTSEG, %ax
    movw %ax, %ds
    movw $INITSEG, %ax
    movw %ax, %es
    movw $256, %cx
    subw %si, %si
    subw %di, %di 

    # 重复执行movsw256次
    rep # 重复后面的指令，次数由cx寄存器中的数确定
    movsw # 将DS+SI指向地址的内容复制到ES+DI指定的地址，长度为一个字

    jmpl $INITSEG, $go 

go:
    movw %cs, %ax
    movw %ax, %ds
    movw %ax, %es
    movw %ax, %ss 
    movw $0xFF0, %sp 

# 通过中断0x13从磁盘扇区加载setup扇区，如果加载失败，则重试
load_setup:
    movw $0x0000, %dx
    movw $0x0002, %cx
    movw $0x0200, %bx
    movb $SETUPLEN, %al 
    movb $0x012, %ah
    int $0x13
    jnc ok_load_setup
    movw $0x0000, %dx
    movw $0x0000, %ax
    int $0x13
    jmp load_setup

# 通过0x10中断在屏幕上显示消息
ok_load_setup:
    movw $msg, %ax 
    movw %ax, %bp 
    movw $0x01301, %ax
    movw $21, %cx
    movb $0, %dl
    int $0x010

    jmpl $SETUPSEG, $0

msg:
.ascii "Setup has been loaded"

.org 508
root_dev:
    .word ROOT_DEV
boot_flag:
    .word 0xaa55

    
